﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Service_monitoring.Utils.WindowsHelper
{
    internal class ViewOperate
    {
        [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
        private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        /// <summary>
        /// 关闭显示窗口传参
        /// </summary>
        public const uint WM_SYSCOMMAND = 0x0112;

        /// <summary>
        /// 关闭显示窗口传参
        /// </summary>
        public const uint SC_MONITORPOWER = 0xF170;

        /// <summary>
        /// 关闭窗口传参
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, uint wParam, int lParam);

        /// <summary>
        /// 锁定计算机
        /// </summary>
        [DllImport("user32.dll")]
        public static extern void LockWorkStation();

        /// <summary>
        /// 控制台标题文字
        /// </summary>
        public static string SetConsoleTitle = "set_console";

        /// <summary>
        /// 隐藏程序控制台
        /// </summary>
        public static void HideConsole()
        {
            Console.Title = SetConsoleTitle;
            IntPtr intptr = FindWindow("ConsoleWindowClass", SetConsoleTitle);
            if (intptr != IntPtr.Zero)
            {
                ShowWindow(intptr, 0);//隐藏这个窗口
            }
        }

        /// <summary>
        /// 显示程序控制台
        /// </summary>
        public static void ShowConsole()
        {
            Console.Title = SetConsoleTitle;
            IntPtr intptr = FindWindow("ConsoleWindowClass", SetConsoleTitle);
            if (intptr != IntPtr.Zero)
            {
                ShowWindow(intptr, 1);//隐藏这个窗口
            }
        }

        /// <summary>
        /// 显示控制栏和菜单键
        /// </summary>
        public static void ShowTaskbar()
        {
            IntPtr trayHwnd = FindWindow("Shell_TrayWnd", null);
            IntPtr hStar = FindWindow("Button", null);
            IntPtr desktopPtr = FindWindow("Progman", null);
            if (trayHwnd != IntPtr.Zero)
            {
                ShowWindow(desktopPtr, 1);//隐藏桌面图标 （0是隐藏，1是显示）
                ShowWindow(trayHwnd, 1);//隐藏任务栏
                ShowWindow(hStar, 1);//隐藏windows 按钮
            }
        }

        /// <summary>
        /// 隐藏控制栏和菜单键
        /// </summary>
        public static void HideTaskbar()
        {
            IntPtr trayHwnd = FindWindow("Shell_TrayWnd", null);
            IntPtr hStar = FindWindow("Button", null);
            IntPtr desktopPtr = FindWindow("Progman", null);
            if (trayHwnd != IntPtr.Zero)
            {
                ShowWindow(desktopPtr, 0);//隐藏桌面图标 （0是隐藏，1是显示）
                ShowWindow(trayHwnd, 0);//隐藏任务栏
                ShowWindow(hStar, 0);//隐藏windows 按钮
            }
        }
    }
}